package leetcode

//https://leetcode.com/problems/split-linked-list-in-parts/
//todo
fun main(args: Array<String>) {

}

fun splitListToParts(root: ListNode?, k: Int): Array<ListNode?> {

    val result = Array<ListNode?>(k, { null })
    var len = 0
    var header = root
    while (header != null) {
        len++
        header = header.next
    }

    val partLen = Math.max(len / k, 1)
    var counter = 0

    var first = header
    val etc = len % k
    if (etc > 0) {
        if (counter < etc - 1) {//先添加头部多出的节点
            header = header?.next
            counter++
        }
        result[0] = first
    }


    for (i in 0 until k) {
        counter = 0
        first = header
        if (result[i] == null)
            result[i] = first
        while (counter < partLen) {
            if (header != null)
                header = header.next
            counter++
        }
        if (header != null) {

        }


    }


    return result

}